home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / hgpltopi.sit / hp2pict deck / card_6097.txt < prev    next >
Encoding:
Text File  |  1990-07-09  |  15.4 KB  |  604 lines

  1. -- card: 6097 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 2253
  5. -- name: hp2pict.inc.c
  6.  
  7.  
  8. -- part contents for background part 1
  9. ----- text -----
  10. hp2pict.inc.c
  11.  
  12. -- part contents for background part 6
  13. ----- text -----
  14. /* hp2pict.inc.c
  15.  *
  16.  *
  17.  * Copyright 1990 by d.g.gilbert.
  18.  * dogStar Software && Indiana University Biology Dept.
  19.  * email: gilbertd@iubio.bio.indiana.edu
  20.  *
  21.  */
  22.  
  23.  
  24.  
  25. /* hp2pict routines & data */
  26.  
  27. /* A4: 10800 x, 7680 y -- GCG driver: 10170-44x, 7840-384y */
  28. /* A3: 15200 x, 10800 y -- GCG driver: 16000-0x, 10170-34y*/
  29. /* 9600 x 7200 == PlotGram idea of hp pagesize */
  30. #define hpPageX   10800   /*10800 */     /*10170 - 44 */
  31. #define hpPageY   7680   /*7456*/ /*7680 */
  32.  
  33. #define LBEND     '~' /* LaBel end char, was 3 (^C),
  34.                       need non-control to work in HCard field...
  35.                       'ΓÇá' -- !! HI ascii is missed by this C !! */
  36.  
  37.  
  38. /*
  39.               F*CKING MUST #INCLUDE <MATH.H>
  40.               or get garbage at runtime without warning
  41. */
  42. int  itan2( float x, float y)
  43. {  /* == atan2, but return angle as int degrees (0..360) */
  44. #define  mpi   3.1415926
  45.   int     r;
  46.  
  47.   if (x==0.0 && y==0.0) r = 360;
  48.   else if (x==0.0) r = 0;
  49.   else if (y==0.0) r = 90;
  50.   else r = (180.0/mpi * atan(fabs(x/y)));
  51.   if (y < 0.0) r = 180 - r;
  52.   r = abs(r);
  53.   if (x < 0.0) r = 360 - r;
  54.   return r;
  55. } /* itan2 */
  56.  
  57.  
  58.  
  59. /*!! dashline & dashstop postscript mess up my LN03R ps
  60.      w/ hacked apple ps dictionary, also LaserWRiter refused
  61.      to print it */
  62.  
  63. void dashline( int dashlength)
  64. {
  65. #define DashedLine   180
  66.  
  67.   typedef struct {
  68.     char  offset;
  69.     char  centered;
  70.     char  dashed[2]; /* ?? longer for patterned dash */
  71.     } tDashedLine, *pDashedLine, **hDashedLine;
  72.  
  73.   hDashedLine   hdash;
  74.  
  75.   (Handle) hdash  = NewHandle(sizeof(tDashedLine));
  76.   (**hdash).offset = 0;     /* ?? */
  77.   (**hdash).centered = 1;  /* ?? */
  78.   (**hdash).dashed[0] = 1; /* ?? */
  79.   (**hdash).dashed[1] = dashlength; /* ?? */
  80.   PicComment( DashedLine, GetHandleSize((Handle)hdash),
  81.       (Handle) hdash);
  82.   DisposHandle( (Handle)hdash);
  83. }
  84.  
  85. void dashstop(void)
  86. {
  87. #define DashedStop   181
  88.  
  89.    PicComment( DashedStop, 0, NULL);
  90. }
  91.  
  92. void  lineit(int linetype, int dashsize)
  93. { /* ?? need turtlegraphics to do dashed lines ? */
  94.   /* not working yet */
  95.  
  96.   switch (linetype) {
  97.     case 0:
  98.       /* dashstop(); */
  99.       break;
  100.     default:
  101.     case 1:
  102.     case 2:
  103.     case 3:
  104.     case 4:
  105.     case 5:
  106.     case 6:
  107.       dashsize = dashsize;
  108.       /* dashline(dashsize); */
  109.       break;
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. void  laserline( int num, int denom)
  116. /* set postscript line width (rel to 1/72 dpi);
  117.   use num=1, denom=4 for 1/300 dpi line */
  118. {
  119. #define SetLineWidth    182
  120.   Point   **linesize;
  121.  
  122.   (Handle) linesize = NewHandle(sizeof(Point));
  123.   (**linesize).v = num;
  124.   (**linesize).h = denom;
  125.   PicComment( SetLineWidth, sizeof(Point), (Handle) linesize);
  126.   DisposHandle( (Handle) linesize);
  127. } /*laserline*/
  128.  
  129.  
  130.  
  131.  
  132. void  drawUpstr(int h, int v, int rotnum, char *s)
  133. {
  134. #define maxd  24
  135. #define maxdb 4   /* must be even */
  136. #define maxw  816 /* was 512 */
  137. #define maxwb 102 /* was 64 -- must be even */
  138.  
  139. typedef char  sbitarray[maxd][maxwb];
  140. typedef char  tbitarray[maxw][maxdb];
  141.  
  142. sbitarray *sbits, *sbit2;
  143. tbitarray *tbits;
  144. Ptr       tp;
  145. int       w,d,i,j;
  146. Rect      tor, sor;
  147. FontInfo  fi;
  148. BitMap    smap, smap2, tmap;
  149. GrafPtr   aport, offport;
  150.  
  151.  
  152.   GetPort( &aport); /* !! may be CGrafPort w/ pixmap */
  153.   GetFontInfo(&fi);
  154.   d  = min(maxd-1, fi.ascent + fi.descent + fi.leading);
  155.   w  = min(maxw-1, stringwidth(s));
  156.   /* h  = h - (d / 2); */  /*let h,v == topleft not center*/
  157.   /* v  = v - (w / 2); */
  158.   SetRect( &tor, h, v, h + d, v + w);
  159.   SetRect( &sor, h, v, h + w, v + d);
  160.  
  161.   (Ptr)sbits = NewPtr(sizeof(sbitarray));
  162.   for (i=0; i<sizeof(sbitarray); i++) ((char *)sbits)[i] = 0;
  163.   smap.baseAddr = (Ptr)sbits;
  164.   if ((long)smap.baseAddr % 2 == 1) ++smap.baseAddr; /* if odd(addr) */
  165.   smap.rowBytes  = maxwb;
  166.   SetRect( &smap.bounds, 0, 0, w, d);
  167.  
  168.   switch (rotnum) {
  169.    case 2:
  170.     (Ptr)sbit2 = NewPtr(sizeof(sbitarray));
  171.     for (i=0; i<sizeof(sbitarray); i++) ((char *)sbit2)[i] = 0;
  172.     smap2.baseAddr = (Ptr)sbit2;
  173.     if ((long)smap2.baseAddr % 2 == 1) ++smap2.baseAddr;
  174.     smap2.rowBytes  = maxwb;
  175.     SetRect( &smap2.bounds, 0, 0, w, d);
  176.     break;
  177.  
  178.    case 1:
  179.    case 3:
  180.     (Ptr)tbits = NewPtr(sizeof(tbitarray));
  181.     for (i=0; i<sizeof(tbitarray); i++) ((char *)tbits)[i] = 0;
  182.     tmap.baseAddr = (Ptr)tbits;
  183.     if ((long)tmap.baseAddr % 2 == 1) ++tmap.baseAddr;
  184.     tmap.rowBytes = maxdb;
  185.     SetRect( &tmap.bounds, 0, 0, d, w);
  186.     break;
  187.     }
  188.  
  189.   (Ptr) offport = NewPtr(sizeof(GrafPort)); /* forget CGrafPort */
  190.   OpenPort( offport);
  191.   SetPortBits( &smap);      /* set our offscreen map */
  192.   TextFont( aport->txFont);
  193.   TextSize( aport->txSize);
  194.   TextFace( aport->txFace);
  195.   MoveTo(0, fi.ascent);
  196.   drawstring(s);            /* draw the bugger */
  197.   ClosePort( offport);
  198.   DisposPtr( (Ptr)offport);
  199.   SetPort( aport);
  200.  
  201.   switch (rotnum) {
  202.  
  203.     case 2: /* 180┬░  ???*/
  204.       for (i=0; i < w; i++) {
  205.         for (j = 0; j < d; j++)
  206.           if (BitTst( &((*sbits)[j][0]), i))
  207.             BitSet( &((*sbit2)[d-j][0]), w-i);
  208.           }
  209.       OffsetRect( &sor, -w, 0/*-d*/); /* ? */
  210.       CopyBits( &smap2, &(aport->portBits), &smap2.bounds, &sor,
  211.             srcOr, NULL);
  212.       DisposPtr( (Ptr)sbit2);
  213.       break;
  214.  
  215.     case 0: /* here for completeness, or just drawstring */
  216.       CopyBits( &smap, &(aport->portBits), &smap.bounds, &sor,
  217.             srcOr, NULL);
  218.       break;
  219.  
  220.  
  221.     case 3:  /* = 270┬░, bottom left is start */
  222.       for (i = w - 1; i >= 0; i--) {
  223.         tp = &((*tbits)[w - (i + 1)][0]);
  224.         for ( j = 0; j < d; j++)
  225.           if (BitTst( &((*sbits)[j][0]), i)) BitSet(tp, j);
  226.         }
  227.       OffsetRect( &tor, -d, -w); /* ! */
  228.       CopyBits( &tmap, &(aport->portBits), &tmap.bounds, &tor,
  229.             srcOr, NULL);
  230.       DisposPtr( (Ptr)tbits);
  231.       break;
  232.  
  233.     case 1:   /* = 90┬░, top right is start */
  234.       for (i = 0; i<w; i++) {
  235.         tp = &((*tbits)[i][0]);
  236.         for (j = d - 1; j >= 0; j--)
  237.           if (BitTst( &((*sbits)[j][0]), i)) BitSet(tp, d - (j + 1));
  238.         }
  239.       CopyBits( &tmap, &(aport->portBits), &tmap.bounds, &tor,
  240.             srcOr, NULL);
  241.       DisposPtr( (Ptr)tbits);
  242.       break;
  243.     }
  244.  
  245.   DisposPtr( (Ptr)sbits);
  246. } /* drawUpstr */
  247.  
  248.  
  249.  
  250. void textPicDraw(int h, int v, int rot, char *s)
  251. {
  252. /* MacDraw standard drawString w/ rotation
  253.   h,v = _center_ of text rect
  254. */
  255. #define textBegin     150
  256. #define textEnd       151
  257. #define textCenter    154
  258.  
  259.   typedef struct {
  260.     char  tjus;     /* 0..4 == left,cent,right,full just */
  261.     char  tflip;    /* 0..2 == 0,horiz,vert flip */
  262.     short trot;     /* 0..360 rotate */
  263.     char  tline;    /* 1,2,3 line spacing */
  264.     char  tcmnt;    /* reserved */
  265.     } TxtPicRec, *pTxtPicRec, **hTxtPicRec;
  266.  
  267.   typedef struct  {
  268.     Fixed y, x;
  269.     } TxtCenter, *pTxtCenter, **hTxtCenter;
  270.  
  271.   int           w, d, rotnum;
  272.   Rect          zerorect;
  273.   FontInfo      fi;
  274.   hTxtPicRec    picdat;
  275.   hTxtCenter    xydat;
  276.   RgnHandle     saveclip;
  277.  
  278.   rotnum = ((rot+44) % 360) / 90;
  279.   GetFontInfo( &fi);
  280.   d = (fi.ascent + fi.descent + fi.leading) / 2;
  281.   w = stringwidth(s) / 2;
  282.   MoveTo( h, v); /* ?? for HP text */
  283.   /* MoveTo(h - w, v - d); */ /* move from center */
  284.  
  285.   (Handle) picdat = NewHandle(sizeof(TxtPicRec));
  286.   (**picdat).tjus = 1;
  287.   (**picdat).tflip = 0;
  288.   (**picdat).trot = rot % 360;
  289.   (**picdat).tline = 1;
  290.   (**picdat).tcmnt = 0;
  291.   PicComment(textBegin, 6, (Handle) picdat);
  292.   DisposHandle( (Handle) picdat);
  293.  
  294.   (Handle)xydat = NewHandle(sizeof(TxtCenter));
  295.   (**xydat).x = Long2Fix(0);
  296.   (**xydat).y = Long2Fix(0);
  297.   PicComment( textCenter, 8, (Handle)xydat);
  298.   DisposHandle( (Handle)xydat);
  299.  
  300.   if (rotnum == 0) drawstring(s);
  301.   else {
  302.     saveclip = NewRgn();
  303.     GetClip( saveclip);
  304.     SetRect( &zerorect, 0,0,0,0);
  305.     ClipRect( &zerorect);
  306.     drawstring(s);
  307.     SetClip( saveclip);
  308.     DisposeRgn( saveclip);
  309.     drawUpstr( h, v, rotnum, s);
  310.     }
  311.   PicComment( textEnd, 0, NULL);
  312. } /*textPicDraw*/
  313.  
  314.  
  315.  
  316.  
  317.  
  318. #define map2pic(x,y,h,v) { \
  319.   h = picrect->left + (x / xscale); \
  320.   v = picrect->top + ((ymax - y) / yscale); \
  321.   }
  322. #define maprel2pic(x,y,h,v) { \
  323.   h = (x / xscale); v = (( - y) / yscale); \
  324.   }
  325.  
  326. PicHandle   hp2pict( Boolean forpage, Rect *picrect, Handle hbuf,
  327.             WindowPtr myWind, Boolean *alldone)
  328. {
  329.   char      *c, *ce, *si, s[256];
  330.   int       cmd, hpx, hpy, x, y, xd, yd, h, v, k, pageno;
  331.   Boolean   dorel, atend, done, moveit, penup;
  332.   Rect      wr, hprect;
  333.   Point     pt;
  334.   float     fx, fy, linedash, xscale, yscale;
  335.   int       linetype, ymax, rot = 0;
  336.   RGBColor  bcolor;
  337.   PicHandle pic;
  338.  
  339.   HLock( hbuf);
  340.   c = (char *) (*hbuf);
  341.   while ((*c != 0) & (*c != ';')) c++;
  342.   if (*c == ';') c++;
  343.   hpx = hpPageX;
  344.   hpy = hpPageY;
  345.  
  346. redraw:
  347.  
  348.   pageno = 0;
  349.   *alldone = false;
  350.   pic = OpenPicture(picrect);
  351.   if (forpage) {
  352.     InsetRect( picrect, 5, 5);
  353.     }
  354.   else {
  355.     InsetRect( picrect, 10, 10);
  356.     OffsetRect( picrect, 0, 5);
  357.     ShowPen();
  358.     if (hasColorQD()) {
  359.       bcolor.red  = 56000;
  360.       bcolor.green= 60000;
  361.       bcolor.blue = 65000;
  362.       if (lotsOfColor(myWind)) RGBBackColor( &bcolor);
  363.       EraseRect( &myWind->portRect);
  364.       }
  365.     }
  366.   SetRect( &wr, -10000, -10000, 30000, 30000);
  367.   ClipRect(&wr);
  368.  
  369.   TextFont(times);
  370.   if (forpage) TextSize(12);
  371.   else TextSize(9);
  372.   laserline(1, 2);
  373.   PenNormal();
  374.   penup = true;
  375.   dorel = false;
  376.   linetype  = 0;
  377.   linedash  = 1;
  378.  
  379.   SetRect( &hprect, 0, 0, hpx, hpy);
  380.   ymax = hpy;
  381.   xscale = hpx / (picrect->right - picrect->left);
  382.   yscale = hpy / (picrect->bottom - picrect->top);
  383.  
  384.   done = (*c == 0);
  385.  
  386.   while (!done) {
  387.     while ((!Isalpha(*c)) && (*c != 0)) c++; /* skip to start of cmd */
  388.     /* ^^ this messes up '\27.' end of transmission */
  389.  
  390.     cmd = (Toupper(*c++))*256 + (Toupper(*c++));
  391.     atend = false;
  392.  
  393.     switch (cmd) {
  394.  
  395.       case '\27.': /* 6958 == '\27.' */ /* not seen ? */
  396.         if (*c == ')') {
  397.           atend = true;
  398.           done = true;
  399.           *alldone = true;
  400.           *c = 0;
  401.           }
  402.         break;
  403.  
  404.       case 'AF':
  405.         /* the end-of-plot signal is "AF;\27.)x" */
  406.         /* multi-page plots will want me to throw up a new
  407.            plot window -> from Draw */
  408.         if ( (++pageno > 1) | (c[1] == '\27' & c[3] == ')') ) {
  409.           atend = true;
  410.           done = true;
  411.           *c = 0;
  412.       /* SysBeep(1); */
  413.           }
  414.         break; /* autopage */
  415.  
  416.       case 'IN':
  417.         /* the start-of-plot signal for GCG is */
  418.         /* "\27.(\27.N;19:\27.I81;;17:IN;...AF;" */
  419.         break; /* initialize */
  420.  
  421.       case 'AA': break; /* arc */
  422.       case 'IP': break; /* frame */
  423.  
  424.       case 'LB':
  425.         for (si=0; *c != LBEND & si < 256; si++, c++)
  426.             s[si] = *c;
  427.         s[si] = 0;
  428.         /* drawstring(s); */
  429.         /*  -- use last valid h,v, rot */
  430.         /* Note: this h,v is left,top? of string, not center */
  431.         textPicDraw( h, v, rot, s);
  432.         ++c; atend = true;
  433.         break; /* LBlabel^C; */
  434.  
  435.       case 'LO':
  436.         x = Strtod( c, &ce); if (ce == c) break;
  437.         c = ce;
  438.         break; /* ? Label Offset move ?? */
  439.  
  440.       case 'LT':
  441.         k = linetype;
  442.         linetype = 0;
  443.         if (*c != ';') {
  444.           linetype = Strtod( c, &ce);
  445.           if (ce != c) {
  446.             c = ce; if (*c == ',') c++;
  447.             linedash = Strtod( c, &ce);
  448.             c = ce;
  449.             }
  450.         }
  451.         if (linetype != k | linetype > 0) {
  452.           maprel2pic(linedash,linedash, xd, yd);
  453.           xd = max( abs(xd), abs(yd));
  454.           lineit( linetype, xd);
  455.           }
  456.         break; /* LT#,#; line type (style, %diag) */
  457.  
  458.       case 'PU':
  459.         penup = true;
  460.         if (*c == ';') break;
  461.         x = Strtod( c, &ce); if (ce == c) break;
  462.         c = ce; if (*c == ',') c++;
  463.         y = Strtod( c, &ce); if (ce == c) break;
  464.         c = ce;
  465.         map2pic( x, y, h, v);
  466.         MoveTo(h,v);
  467.         break; /* PU[#,#];penup */
  468.  
  469.       case 'PR':  /* PR#,#[...]; rel move */
  470.         dorel = true;
  471.         goto penat;
  472.  
  473.       case 'PD':  /* PD[#,#,...]; pendn */
  474.         penup = false;
  475.         goto penat;   /* slide into PA loop */
  476.  
  477.       case 'PA':
  478.         dorel = false;
  479.         moveit = (*c != ',');
  480. penat:
  481.         do {
  482.           if (*c == ',') c++;
  483.           x = Strtod( c, &ce); if (ce == c) break;
  484.           c = ce; if (*c == ',') c++;
  485.           y = Strtod( c, &ce); if (ce == c) break;
  486.           c = ce;
  487.           if (dorel) {
  488.             maprel2pic( x, y, h, v);
  489.             if (penup | moveit) Move(h,v);
  490.             else Line(h,v);
  491.             GetPen(&pt);
  492.             h = pt.h; v = pt.v; /* save for LB */
  493.             }
  494.           else {
  495.             map2pic( x, y, h, v);
  496.             if (penup | moveit) MoveTo(h,v);
  497.             else LineTo(h,v);
  498.             }
  499.           moveit = false;
  500.           } while (true);
  501.         break; /* PA#,#[,#,#...]; mvabs */
  502.  
  503.       case 'RO':
  504.         x = Strtod( c, &ce);
  505.         if ((x > 60 & x < 120) && !forpage) {
  506.           /* can't do for printed page?? -- need to mangle  
  507.              page orientation in pageSetup... */
  508.           hpx = hpPageY;
  509.           hpy = hpPageX;
  510.  
  511.           xd = picrect->right;
  512.           picrect->right = picrect->bottom;
  513.           picrect->bottom = xd;
  514.  
  515.           if (!forpage) HidePen();
  516.           ClosePicture();
  517.           KillPicture( pic);
  518.  
  519.           xd = myWind->portRect.right - myWind->portRect.left;
  520.           yd = myWind->portRect.bottom - myWind->portRect.top;
  521.           SizeWindow( myWind, yd, xd, true);
  522.           BringToFront( myWind);
  523.           goto redraw;
  524.           }
  525.         break; /* RO#; rotate plot */
  526.  
  527.       case 'SP':
  528.         x = Strtod( c, &ce);
  529.         switch (x) {
  530.           /* pen colors should be user option */
  531.           /* this form works w/ PICT1(b/w) as well as PICT2 */
  532.           default:
  533.           case 1: ForeColor(blackColor); break;
  534.           case 2: ForeColor(greenColor); break;
  535.           case 3: ForeColor(blueColor); break;
  536.           case 4: ForeColor(redColor); break;
  537.           case 5: ForeColor(cyanColor); break;
  538.           case 6: ForeColor(magentaColor); break;
  539.           case 7: ForeColor(yellowColor); break;
  540.           case 8: ForeColor(blackColor); break;
  541.           }
  542.         /* set color to # */
  543.         break; /* SP[#]; select pen */
  544.  
  545.       case 'SI':
  546.         if (*c == ';') break;
  547.         fx = Strtod( c, &ce); if (ce == c) break;
  548.         c = ce; if (*c == ',') c++;
  549.         fy = Strtod( c, &ce); if (ce == c) break;
  550.         c =  ce;
  551.         /* map fx,fy into TextSize ?? */
  552.         break; /* SI#,#; set text abs width,abs height ? */
  553.  
  554.       case 'DI':
  555.         rot = 0;
  556.         if (*c == ';') break;
  557.         fy = Strtod( c, &ce); if (ce == c) break;
  558.         c = ce; if (*c == ',') c++;
  559.         fx = Strtod( c, &ce); if (ce == c) break;
  560.         c =  ce;
  561.         rot = itan2( fx, fy);
  562.         break; /* DI#,#; text direction */
  563.               /* (run = cos(direction), rise = sin(direction) */
  564.  
  565.       case 'SL':
  566.         break; /* SL[#]; set text slant (tangent) ? */
  567.  
  568.       case 'SC': break; /* scale */
  569.       case 'VS': break; /* VS#; pen speed */
  570.  
  571.       case 'CI':   /* filled oval */
  572.         x = Strtod( c, &ce); if (ce == c) break;
  573.         c = ce; if (*c == ',') c++;
  574.         y = Strtod( c, &ce); if (ce == c) break;
  575.         c = ce;
  576.         maprel2pic( x+1, y+1, h, v);
  577.         h = min(h,abs(v));  /* h is bad(huge) for card-sized plot */
  578.         GetPen(&pt);
  579.         SetRect(&wr, pt.h, pt.v, pt.h, pt.v);
  580.         InsetRect(&wr, -h, -h);
  581.         /* FillOval(&wr, black); */
  582.         PaintOval(&wr);
  583.         break;
  584.  
  585.       default : break;
  586.       }
  587.  
  588.     if (!atend)
  589.       while ((*c != 0) && (*c != ';') && (!Isalpha(*c)))
  590.         c++; /* skip to end cmd */
  591.     if (*c == ';') c++;
  592.  
  593.     if (*c == 0) done = true;
  594.     }
  595.  
  596.   if (!forpage) HidePen();
  597.   ClosePicture();
  598.   HUnlock( hbuf);
  599.  
  600.   return pic;
  601.  
  602. } /* hp2pict */
  603.  
  604.